﻿using System;
using System.Drawing;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DevExpress.Web.ASPxEditors;
using DevExpress.Web.ASPxGridLookup;
using Microsoft.ApplicationBlocks.Data;
using DevExpressWebApplication1.Utils;
namespace DevExpressWebApplication1.Utils
{
    public class ControlUtils
    {

        public static string GetCBBValue(ASPxComboBox cbb)
        {
            string result = "";
            try
            {
                result = cbb.Value.ToString();
            }
            catch (Exception ex)
            {
                //SiAuto.Main.LogString("Lỗi GetCBBValue :", ex.ToString());
            }

            return result;
        }

        public static void SetCBBValue(ASPxComboBox cbb, object objects)
        {
            try
            {
                if (objects != null)
                {
                    int val;
                    cbb.Value = int.TryParse(objects.ToString(), out val) ? val : objects;
                }
            }
            catch (Exception ex)
            {
                //SiAuto.Main.LogString("Lỗi SetCBBValue :", ex.ToString());
            }
        }

        public static string GetGridLookupValue(ASPxGridLookup glk)
        {
            string result = "";
            try
            {
                result = glk.Value.ToString();
            }
            catch (Exception ex)
            {
                //SiAuto.Main.LogString("Lỗi GetGridLookupValue :", ex.ToString());
            }

            return result;
        }

        public static void SetGridLookupValue(ASPxGridLookup glk, object objects)
        {
            try
            {
                if (objects != null)
                    glk.Value = objects;
            }
            catch (Exception ex)
            {
                //SiAuto.Main.LogString("Lỗi SetGridLookupValue :", ex.ToString());
            }
        }

        public static string GetDateEditValue(ASPxDateEdit de)
        {
            string result = "";
            try
            {
                result = DateTime.Parse(de.Value.ToString()).ToString("MM/dd/yyyy");
            }
            catch (Exception ex)
            {
                //SiAuto.Main.LogString("Lỗi GetDateEditValue:", ex.ToString());
            }

            return result;
        }
        public static string GetDateEditValueForCompare(ASPxDateEdit de)
        {
            string result = "";
            try
            {
                result = DateTime.Parse(de.Value.ToString()).ToString("dd/MM/yyyy");
            }
            catch (Exception ex)
            {
                //SiAuto.Main.LogString("Lỗi GetDateEditValueForCompare:", ex.ToString());
            }

            return result;
        }
        public static void SetDateEditValue(ASPxDateEdit de, object objects)
        {
            try
            {
                if (objects != null)
                    de.Value = DateTime.Parse(objects.ToString());
            }
            catch (Exception ex)
            {
                //SiAuto.Main.LogString("Lỗi SetDateEditValue:", ex.ToString());
            }
        }
        public static string GetTimeEditValue(ASPxTimeEdit de)
        {
            string result = "";
            try
            {
                result = de.Text;
            }
            catch (Exception ex)
            {
                //SiAuto.Main.LogString("Lỗi GetTimeEditValue :", ex.ToString());
            }

            return result;
        }

        public static void SetTimeEditValue(ASPxTimeEdit de, object objects)
        {
            try
            {
                if (objects != null)
                    de.Value = objects.ToString();
            }
            catch (Exception ex)
            {
                //SiAuto.Main.LogString("Lỗi SetTimeEditValue :", ex.ToString());
            }
        }

        public static void SetTextBoxValue(ASPxTextBox Textbox, object objects)
        {
            try
            {
                if (objects != null)
                    Textbox.Text = objects.ToString();
            }
            catch (Exception ex)
            {
                //SiAuto.Main.LogString("Lỗi SetTextBoxValue :", ex.ToString());
            }
        }

        public static void SetMemoValue(ASPxMemo Memo, object objects)
        {
            try
            {
                if (objects != null)
                    Memo.Text = objects.ToString();
            }
            catch (Exception ex)
            {
                //SiAuto.Main.LogString("Lỗi SetMemoValue :", ex.ToString());
            }
        }

        public static string GetLookupValue(ASPxGridLookup cbb)
        {
            string result = "";
            try
            {
                result = cbb.Value.ToString();
            }
            catch (Exception ex)
            {
                //SiAuto.Main.LogString("Lỗi GetLookupValue :", ex.ToString());
            }

            return result;
        }

        public static void SetLookupValue(ASPxGridLookup cbb, object objects)
        {
            try
            {
                if (objects != null)
                    cbb.Value = objects;
            }
            catch (Exception ex)
            {
                //SiAuto.Main.LogString("Lỗi SetLookupValue :", ex.ToString());
            }
        }
        public static string GetSpinValue(ASPxSpinEdit spin)
        {
            string result = "";
            try
            {
                result = spin.Value.ToString();
            }
            catch (Exception ex)
            {
                //SiAuto.Main.LogString("Lỗi GetSpinValue :", ex.ToString());
            }

            return result;
        }

        public static void SetSpinValue(ASPxSpinEdit spin, object objects)
        {
            try
            {
                if (objects != null)
                {
                    spin.Value = objects;

                }
            }
            catch (Exception ex)
            {
                //SiAuto.Main.LogString("Lỗi SetSpinValue :", ex.ToString());
            }

        }

        public static string getCheckBoxValue(ASPxCheckBox chk)
        {
            string result = "";
            try
            {
                result = chk.Value.ToString();
            }
            catch (Exception ex)
            {
                //SiAuto.Main.LogString("Lỗi getCheckBoxValue :", ex.ToString());
            }

            return result;
        }

        public static void SetCheckBoxValue(ASPxCheckBox chk, object objects)
        {
            try
            {
                if (objects != null)
                {
                    bool state = false;
                    bool.TryParse(objects.ToString(), out state);
                    chk.Checked = state;
                }
            }
            catch (Exception ex)
            {
                //SiAuto.Main.LogString("Lỗi SetCheckBoxValue :", ex.ToString());
            }
        }

        public static void SetImageValue(ASPxImage img, object objects)
        {
            try
            {
                if (objects != null)
                {
                    img.ImageUrl = objects.ToString();
                }
            }
            catch (Exception ex)
            {
                //SiAuto.Main.LogString("Lỗi SetImageValue :", ex.ToString());
            }
        }

        public static int getBOID()
        {

            return int.Parse(HttpContext.Current.Session["BOID"].ToString());
        }

        public static int getuserid()
        {
            return int.Parse(HttpContext.Current.Session["userid"].ToString());
        }

   
        public static void addButton4Cmb(System.Web.UI.Page mypage, ASPxComboBox cmb, int Buttontype, string popupID)
        {
            string prefixCookie = mypage.GetType().ToString().Replace("ASP.", "");
            switch (Buttontype)
            {
                case 0://add cả 2 button
                    AddFavButton4Cmb(cmb);
                    AddNewButton4Cmb(cmb);
                    cmb.ClientSideEvents.Init = "function(s,e){ GetDefaultValue4Cmb(s,'" + prefixCookie + "_" + cmb.ClientInstanceName + "'); }";
                    cmb.ClientSideEvents.ButtonClick = "function(s,e){ if( e.buttonIndex == 0){setCookie('" + prefixCookie + "_" + cmb.ClientInstanceName + "',s.GetValue(),365);} else { popupWindow4Cmb('" + popupID + "');} }";
                    break;
                case 1://chi add button new
                    AddNewButton4Cmb(cmb);
                    cmb.ClientSideEvents.ButtonClick = "function(s,e){ popupWindow4Cmb('" + popupID + "'); }";
                    break;
                case 2://chỉ add button fav
                    AddNewButton4Cmb(cmb);
                    cmb.ClientSideEvents.Init = "function(s,e){ GetDefaultValue4Cmb(s,'" + prefixCookie + "_" + cmb.ClientInstanceName + "'); }";
                    cmb.ClientSideEvents.ButtonClick = "function(s,e){ setCookie('" + prefixCookie + "_" + cmb.ClientInstanceName + "',s.GetValue(),365); }";
                    break;
            }
            /*
             * <ButtonStyle BackColor="Transparent">
                            <Paddings Padding="0px" />
                            <Border BorderStyle="None" />
                        </ButtonStyle>
             */
            cmb.ClientSideEvents.GotFocus = "function(s,e){ s.SelectAll(); }";
            cmb.EnableDefaultAppearance = false;
            //cmb.ButtonStyle.BackColor = Color.Transparent;
            //cmb.ButtonStyle.Width = Unit.Pixel(15);
            //cmb.ButtonStyle.Paddings.Padding = Unit.Pixel(0);
            //cmb.ButtonStyle.Border.BorderStyle = BorderStyle.None;
            //cmb.SkinID = "cmbDefault";
        }

        /*
         <dx:EditButton Position="Left" ToolTip="<%$ Resources:Control, DefaultButton.Tooltip %>">
                                    <Image Height="16px" Url="~/images/btnico/star.png" Width="16px">
                                    </Image>
                                </dx:EditButton>
                                <dx:EditButton ToolTip="<%$ Resources:Control, AddButton.Tooltip %>">
                                    <Image Url="~/images/common/Add.png" Width="16px">
                                    </Image>
                                </dx:EditButton>
         */

        public static void AddNewButton4Cmb(ASPxComboBox cmb)
        {
            var btnNew = new EditButton { Position = ButtonsPosition.Right };
            btnNew.Image.Url = "~/images/common/Add.png";
            btnNew.Image.Width = Unit.Pixel(16);
            btnNew.ToolTip = "Nhấn vào đây để tạo mới thêm danh mục này";

            cmb.Buttons.Add(btnNew);
        }
        public static void AddFavButton4Cmb(ASPxComboBox cmb)
        {
            var btnFav = new EditButton { Position = ButtonsPosition.Left };
            btnFav.Image.Url = "~/images/btnico/star.png";
            btnFav.Image.Width = Unit.Pixel(16);
            btnFav.ToolTip = "Nhấn vào đây để chọn mục này là mặc định cho lần sau.";
            cmb.Buttons.Add(btnFav);
        }
    }
}